package com.thp.bigdata.rjoin.findFriends;

import java.io.IOException;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

public class CommonFriendsStepOneMapper extends Mapper<LongWritable, Text, Text, Text> {
	@Override
	protected void map(LongWritable key, Text value, Context context)
			throws IOException, InterruptedException {
		// value  -  "A:B,C,D,E,F,G"
		String line = value.toString();
		// 分割字符串得到用户和好友们  为 {"A","B,C,D,E,F,G"}
		String[] userAndFriends = line.split(":");
		// 获取用户
		String user = userAndFriends[0];
		// 获取朋友们
		String[] friends = userAndFriends[1].split(",");
		// 循环遍历，以 <B,A> <C,A> <D,A> <E,A> <F,A> <G,A> ... 的形式传给reducer
		for(String friend : friends) {
			context.write(new Text(friend), new Text(user));
		}
	}
}
